package xyz.ddlnt.servicegeneral.config;

import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.messages.Bucket;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import xyz.ddlnt.servicegeneral.config.configProperties.MinIOConfigProperties;

import java.util.List;

/**
 * @author ZZULI_SE 210910
 * @data 2025/4/23 17:03
 */
@Slf4j
@Component
@Configuration
public class MinIOClientConfig {
    @Resource
    private MinIOConfigProperties minIOConfigProperties;


    @Getter
    private static MinioClient minioClient;


    /**
     * 判断 bucket是否存在
     * @param bucketName: 桶名
     */
    @SneakyThrows(Exception.class)
    public static boolean bucketExists(String bucketName) {
        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    }


    /**
     * 获取全部bucket
     *
     * @param :
     * @return: java.util.List<io.minio.messages.Bucket>
     * @date : 2020/8/16 23:28
     */
    @SneakyThrows(Exception.class)
    public static List<Bucket> getAllBuckets() {
        return minioClient.listBuckets();
    }

    /**
     * 初始化minio配置
     *
     * @param :
     * @return: void
     * @date : 2020/8/16 20:56
     */
    @PostConstruct
    public void init() {
        try {
            minioClient = MinioClient.builder()
                    .endpoint(minIOConfigProperties.getUrl())
                    .credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())
                    .build();
        } catch (Exception e) {
            log.error("初始化minio配置异常: ", e.fillInStackTrace());
        }
    }
}
